﻿
Imports System.IO
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl

'contains
'homepage
'random  ?
'what's new
'jump to topic
'jump to category
'recent topic
'recent category
'search
'

Public Class header
    Inherits container

    Private Shared css As String = Nothing
    Public Const containerId As String = "hd"
    Public Const menuId As String = "mu"
    Public Const linkClass As String = "l"
    Public Const separatorClass As String = "s"

    Public Class datasource
        Public page As xhtmlpage = Nothing
        Public addvisitCounter As Boolean = True
    End Class

    Private Shared Function createSeparator() As span
        Dim context As httpContext = Nothing
        context = httpServer2.currentContext()
        Dim rtn As span = Nothing
        rtn = context.createControl(New span())
        rtn.text = "|"
        rtn.class = separatorClass
        Return rtn
    End Function

    Private Shared Function createLink(ByVal inlineScript As inlineScript, _
                                       ByVal url As String, ByVal text As String, _
                                       Optional ByVal onclick As String = Nothing) As a
        Dim context As httpContext = Nothing
        context = httpServer2.currentContext()

        Dim rtn As a = Nothing
        If isemptystring(url) Then
            rtn = context.createControl(New emptyA())
        Else
            rtn = context.createControl(New a())
            rtn.href = url
        End If
        If Not isemptystring(onclick) Then
            rtn.onclick = onclick
        End If
        rtn.text = text
        rtn.class = linkClass
        'colorfulControl.invoke(inlineScript, rtn)

        Return rtn
    End Function

    Public Const functionName As String = "whdprm"

    Private Shared Sub promptRegister(ByVal inlineScript As inlineScript)
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        'b = baseurl, k = key, t = title
        inlineScript.addscript("wfileis.header.prompt", "function " + functionName + "(b,k,t){" + _
                               prompt2.invoke(inlineScript, "function(" + prompt2.inputBoxResult + "){" + _
                                              locationhref.invoke(inlineScript, _
                                                                  "b+'?'+k+'='+" + _
                                                                  encodeURIComponent.invoke(inlineScript, _
                                                                                            prompt2.inputBoxResult)) + _
                                              ";}", _
                                              title:="t") + ";}")
    End Sub

    Private Shared Function promptInvoke(ByVal inlineScript As inlineScript, _
                                         ByVal baseurl As String, ByVal key As String, ByVal title As String, _
                                         Optional ByRef actionTarget As String = Nothing) As String
        If Not inlineScript Is Nothing Then
            promptRegister(inlineScript)
        End If

        actionTarget += functionName + "(" + baseurl + "," + key + "," + title + ")"
        If supportIE6() Then
            actionTarget += ";return " + webctrl.[false].name(inlineScript) + ";"
        End If
        Return actionTarget
    End Function

    Private Shared Function createActionlink(ByVal inlineScript As inlineScript, ByVal text As String, _
                                             ByVal titleVar As String, ByVal titleText As String, _
                                             ByVal baseUrl As String, ByVal key As String) As a
        assert(Not inlineScript Is Nothing, "inlineScript is nothing.")
        webctrl.var.invoke(inlineScript, titleVar, "'" + titleText + "'")
        Return createLink(inlineScript, Nothing, text, promptInvoke(inlineScript, _
                                                                    "'" + baseUrl + "'", _
                                                                    "'" + key + "'", titleVar))
    End Function

    Public Sub setData(ByVal ds As datasource)
        assert(Not ds Is Nothing, "ds is nothing.")
        assert(Not ds.page Is Nothing, "ds.page is nothing.")

        If isdebugmode() Then
            readcss()
        End If

        ds.page.inlineStyle().addstyle("wfileis_header_css", css)
        ds.page.inlineStyle().addstyle("body.background-color", _
                                       "BODY{background-color:" + _
                                           Convert.ToString(colorfulControl.defaultOriginColor) + ";}")

        Dim context As httpContext = Nothing
        context = httpServer2.currentContext()

        Dim container As span = Nothing
        container = context.createControl(New span())
        container.id = containerId
        addchild(container)

        container.addchild(above_header.create(context, ds.page))

        Dim menu As div = Nothing
        menu = context.createControl(New div())
        menu.id = menuId
        container.addchild(menu)

        menu.addchild(createLink(ds.page.inlineScript(), _
                                      generateLink(context, network.thislevelPathMark, Nothing), _
                                      context.string(stringId.HEADER_HOMEPAGE)))
        menu.addchild(createSeparator())
        menu.addchild(createLink(ds.page.inlineScript(), _
                                      generateLink(context, _
                                                   strmid(whatsnew._pathidentity, strlen(network.pathSeperator)), _
                                                   Nothing), _
                                      context.string(stringId.HEADER_WHATSNEW)))
        menu.addchild(createSeparator())
        menu.addchild(createLink(ds.page.inlineScript(), _
                                      generateLink(context, _
                                                   strmid(writerpage._pathidentity, strlen(network.pathSeperator)), _
                                                   Nothing), _
                                      context.string(stringId.HEADER_WRITER)))
        menu.addchild(createSeparator())
        menu.addchild(createLink(ds.page.inlineScript(), _
                                      strmid(random._pathidentity, strlen(network.pathSeperator)), _
                                      context.string(stringId.HEADER_RANDOM)))
        menu.addchild(createSeparator())
        Const totopicTitleVar As String = "whttt"
        menu.addchild(createActionlink(ds.page.inlineScript(), context.string(stringId.HEADER_TOTOPIC), _
                                            totopicTitleVar, context.string(stringId.HEADER_TOTOPIC_TITLE), _
                                            strmid(view._pathidentity, strlen(network.pathSeperator)), query.key))
        menu.addchild(createSeparator())
        Const tocategoryTitleVar As String = "whtct"
        menu.addchild(createActionlink(ds.page.inlineScript(), context.string(stringId.HEADER_TOCATEGORY), _
                                            tocategoryTitleVar, context.string(stringId.HEADER_TOCATEGORY_TITLE), _
                                            strmid(catView._pathidentity, strlen(network.pathSeperator)), _
                                            query.category))
        menu.addchild(createSeparator())
        menu.addchild(createLink(ds.page.inlineScript(), _
                                      strmid(rss._pathidentity, strlen(network.pathSeperator)), _
                                      context.string(stringId.HEADER_RSS)))
#If RETIRED Then
        container.addchild(createSeparator())
        container.addchild(createLink(ds.page.inlineScript(), _
                                      generateLink(context, _
                                                   strmid(recentTopic._pathidentity, strlen(network.pathSeperator)), _
                                                   Nothing), _
                                      context.string(stringId.HEADER_RECENT_TOPIC)))
        container.addchild(createSeparator())
        container.addchild(createLink(ds.page.inlineScript(), _
                                      generateLink(context, _
                                                   strmid(recentCategory._pathidentity, strlen(network.pathSeperator)), _
                                                   Nothing), _
                                      context.string(stringId.HEADER_RECENT_CATEGORY)))
#End If

        If ds.addvisitCounter Then
            Dim div As div = Nothing
            div = context.createControl(New div())
            div.class = "vc"
            container.addchild(div)

            addvisitCounter(context, div)
        End If
    End Sub

    Public Sub New(ByVal ds As datasource, Optional ByVal buff As MemoryStream = Nothing)
        Me.New(buff)
        setData(ds)
    End Sub

    Public Sub New(Optional ByVal buff As MemoryStream = Nothing)
        MyBase.New(buff)
    End Sub

    Private Shared Sub readcss()
        css = cssrequest.readcss("header.css")
    End Sub

    Shared Sub New()
        readcss()
    End Sub

    Public Shared Function generateInstance(ByVal page As xhtmlpage, _
                                            Optional ByVal addvisitCounter As Boolean = True) As header
        Dim context As httpContext = Nothing
        context = httpServer2.currentContext()

        Dim hd As header = Nothing
        hd = context.createControl(New header())
        Dim ds As header.datasource = Nothing
        ds = New header.datasource()
        ds.page = page
        ds.addvisitCounter = addvisitCounter
        hd.setData(ds)

        Return hd
    End Function
End Class
